package com.qf.controller;

import com.qf.config.DeadLetterConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送消息到normal_exchange
 */
@RestController
public class DeadController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/dead/{expire}")
    public String dead(@PathVariable String expire){
        String msg = "我要变成死信!!!!";
        rabbitTemplate.convertAndSend(DeadLetterConfig.NORMAL_EXCHANGE, "", msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(expire);
                return message;
            }
        });
        System.out.println("发消息时间:" + System.currentTimeMillis());
        return "OK!!";
    }
}
